package chen.bupt.vsmtest;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import chen.bupt.util.HDFSFileUtils;

public class GetUserVector {
	private static String outputPath = "/user/chenlingpeng/smth/user/ballard_vector";
	private static String inputPath = "/user/chenlingpeng/smth/uservector";

	public static class Map1 extends Mapper<LongWritable, Text, Text, NullWritable> {

		public void map(LongWritable key, Text value, Context context)
				throws IOException, InterruptedException {
			if(!HDFSFileUtils.isInputFile(context))return;
			if(value.toString().startsWith("ballard|")){
				context.write(value, NullWritable.get());
			}
		}
	}

	public static class Reduce1 extends Reducer<Text, Text, Text, NullWritable> {
		public void reduce(Text key, Iterable<Text> values, Context context)
				throws IOException, InterruptedException {}
	}

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		conf.set("mapred.task.timeout", "0");
		Job job = new Job(conf, "VSMTest");
		job.setJarByClass(VSMTest.class);
		job.setMapperClass(Map1.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(NullWritable.class);
		Path input = new Path(inputPath);
		Path output = new Path(outputPath);
		HDFSFileUtils.deleteFile(output, conf);
		FileInputFormat.addInputPath(job, input);
		FileOutputFormat.setOutputPath(job, output);
		System.exit(job.waitForCompletion(true) ? 0 : 1);
	}

}
